<template>
    <div class="login-box-title" style="height: 100px;">
        <div class="logoBig">
            <img src="~images/logo.png" style="margin-left: 0 ; height: 100px;" alt="">
        </div>
    </div>
    <div class="login-banner" >
        <div class="login-main" >
            <div class="login-banner-bg"><img src="~images/big.jpg" /></div>
            <div class="login-box">
                <h3 class="title">登录商城</h3>
                <div class="login-form">
                    <el-form ref="ruleFormRef" :model="loginForm" :rules="rules">
                        <el-form-item prop="username">
                            <el-input v-model="loginForm.username" placeholder="邮箱/手机/用户名" />
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" />
                        </el-form-item>
                    </el-form>
                </div>
                <div class="login-links" >
                    <el-checkbox v-model="rememberPassword" label="记住密码" size="large" />
                    <span class="am-fr">忘记密码</span>
                </div>
                <div class="am-cf">
                    <el-button style="width: 100%;" type="primary" @click="login(ruleFormRef)">登 录</el-button>
                </div>
                <div class="am-cf">
                    <el-button style="width: 100%;" @click="register">注 册</el-button>
                </div>
            </div>
        </div>
    </div>
    <div class="login-info">
        <div class="am-fr">联系我们</div>
        <div class="am-fr">网站地图</div>
    </div>
</template>
<script setup lang="ts">
    import {loginApi} from '@/api/user/login'
    import {useRouter} from "vue-router";
    import {reactive, ref} from "vue";
    import {ElMessage, FormInstance} from "element-plus";
    import store from "@/store";

    const router = useRouter()
    const ruleFormRef = ref<FormInstance>()
    const rules = reactive({
        username: [
            { required: true, message: '请输入邮箱/手机/用户名', trigger: 'blur' },
            { min: 4, max: 20, message: '账号长度必须为4-20位字符!', trigger: 'blur' }
        ],
        password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 16, message: '密码长度必须为6-16位字符!', trigger: 'blur' }
        ]
    })

    const loginForm = reactive({
        username: '',
        password: ''
    })

    const rememberPassword = ref(true)

    const login = async (formEl: FormInstance | undefined) => {
        if(!formEl) return
        await formEl.validate(valid => {
            if(valid) {
                const obj = {
                    username: loginForm.username,
                    password: loginForm.password
                }
                loginApi(obj).then(res => {
                    if(res && res.data.code === 200) {
                        store.commit('changeToken', res.data.msg)
                        store.commit("changeUserInfo", JSON.stringify(res.data.data))
                        router.push('/snow.html')
                    }
                    // ElMessage.error(res.data.msg || '登录失败')
                })
            }
        })
    }
    const register = () => {
        router.push('/register.html')
    }
</script>
<style lang="scss" scoped>
.login-box-title{
    max-width: 1000px;
    margin: 0 auto;
    height: 60px;
    img{
        height:60px;
    }
}
.am-cf{
    margin: 20px 0;
    width: 100%;
}

.login-links{
    margin: 20px 0;
    font-size:14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.login-info{
    margin: 20px;
    font-size:14px;
    text-align: left;
    display: flex;
}

.am-fr{
    margin-left: 10px;
    cursor: pointer;
    height: 40px;
    line-height: 40px;
    color: #409eff;
    text-decoration: underline;
}

@media screen and (min-width:1025px){
    .login-box{
        width: 100%;
        max-width:360px;
        height: 410px;
        position: absolute;
        margin-top:20px;
        margin-left:0;
        background:#f8f8f8;
        right:2%;
        padding:10px 20px;
    }
    .login-banner{
        width:100%;
        height:470px;
        background:#fd7a72;
    }
    .login-main{
        max-width:1000px;
        height:470px;
        margin:0 auto;
        position:relative;
    }
    /*背景*/
    .login-banner-bg{
        display:block;
        float:left;
    }
}
</style>